package pl.us.ij.plugins.segmentation.forms;

import ij.ImagePlus;

/**
 * Created by IntelliJ IDEA.
 * User: pgajdzik
 * Date: Aug 18, 2010
 * Time: 4:14:22 PM
 * To change this template use File | Settings | File Templates.
 */
public class ImagePlusInfo {
    private ImagePlus imagePlus;

    private float max;
    private float min;

    public ImagePlusInfo(ImagePlus imp) {
        this.imagePlus = imp;

        analyseImage();
    }

    public float getMax() {
        return this.max;
    }

    public float getMin() {
        return this.min;
    }

    public float fitRange(float value) {
        return value < min
                ? min
                : value > max
                ? max
                : value;
    }

    private void analyseImage() {
        findMinAndMax((float[]) this.imagePlus.getProcessor().convertToFloat().getPixels());
    }

    private void findMinAndMax(float[] pixels) {
        this.min = Float.MAX_VALUE;
        this.max = Float.MIN_VALUE;

        for (int i = 0; i < pixels.length; i++) {
            if (max < pixels[i])
                max = pixels[i];
            else if (min > pixels[i])
                min = pixels[i];
        }
    }
}